<template>
  <div>
    <RPButton optType="create" type="info" text="刷新组织-/bpmp/org/refresh" @click="refresh" />
  </div>
</template>

<script>
import bpmpCenterApi from '@/api/bpmCenter.js'
import mgtApi from '@/api/mgt.js'
export default {
  name: 'formManage',
  data() {
    return {}
  },
  methods: {
    refresh() {
      this.getOrgList()
      bpmpCenterApi
        .bpmpOrgRefresh([
          '/CRC/CRC00001',
          '/CRC/CRC000003',
          '/CRC/CRC000013',
          '/CRC/CRC000004',
          '/CRC/CRC000005',
          '/CRC/CRC000006',
          '/CRC/CRC000007',
          '/CRC/CRC000002',
          '/CRC/CRC000008',
          '/CRC/CRC000009',
          '/CRC/CRC000010',
          '/CRC/CRC000011',
          '/CRC/CRC000012',
          '/CRC',
          '/CRB',
          '/CRV',
          '/CRBEV',
          '/CRNF',
          '/CRP',
          '/CRGAS',
          '/CRLD',
          '/CRCEM',
          '/CRPRO',
          '/CRPH',
          '/CRPCG',
          '/CRSJ',
          '/CRDC',
          '/CRJZ',
          '/CRHC',
          '/CRCAP',
          '/CRBK',
          '/CRCT',
          '/CRCAS',
          '/CRCAPM',
          '/CRE',
          '/CRMIC',
          '/CRCC',
          '/CRCEPT',
          '/CRDIGITAL',
          '/CRCS'
        ])
        .then(res => {})
    },
    async getOrgList() {
      try {
        // 获取根节点
        const root = await mgtApi.getAuthorizedOrgs('')
        await mgtApi.getRefresh('')
        if (!root || root.length === 0) {
          console.error('No root organization found.')
          return
        }

        let orgDatas = []
        // 获取一级业务单元
        const org = await mgtApi.getAuthorizedOrgs(root[0].orgPath)
        await mgtApi.getRefresh(root[0].orgPath)
        orgDatas = org

        // 获取集团总部（假设是 orgDatas[0] 的子节点）
        if (orgDatas.length > 0) {
          const item = await mgtApi.getAuthorizedOrgs(orgDatas[0].orgPath)
          await mgtApi.getRefresh(orgDatas[0].orgPath)
          orgDatas[0].children = item
        } else {
          console.error('No first-level organization found.')
        }
      } catch (error) {
        console.error('Error fetching organization data:', error)
      }
    }
  },
  mounted() {}
}
</script>
